#include "appviewimpl.h"

CAppViewImpl::CAppViewImpl(void)
{

}
CAppViewImpl::~CAppViewImpl(void)
{
	logi("CAppViewImpl::~CAppViewImpl");
}
std_method_impl CAppViewImpl::Init(int Argc, basic_tchar* Argv[], IBase* pRot, IBase* pBase)
{
	rc_assert(pRot != NULL, E_FAIL)
	return pRot->QueryInterface(IID_IComRotMessage, (void**)&m_pRot);
}
std_method_impl CAppViewImpl::Uninit()
{
	HRESULT		hr = S_OK;

	m_pRot.dispose();

	return hr;
}
std_method_impl CAppViewImpl::Start(_pinstance hInstance, UINT uType)
{
	HRESULT		hr = S_OK;

	HttpInstancePtr			m_pIHttp;

	_lComPtr<INet>	pINet;

	m_pRot->GetObject(CLSID_INet, IID_INet, (IBase**)&pINet.m_p);
	rc_assert(pINet.m_p != INULL, E_FAIL)

	//DnsPtr	pIDns;

	//pINet->CreateProtocol(Protocol_dns, (IBase**)&pIDns);

	//pIDns->Send("www.baidu.com", NULL);

	//pIDns->Close();

	//pINet.dispose();

	pINet->CreateProtocol(Protocol_http, (IBase**)&m_pIHttp);

	m_pIHttp->Cancel();

	m_pIHttp.dispose();

	InitBus();

	return hr;
}
std_method_impl CAppViewImpl::Stop(UINT uExitCode)
{
	HRESULT		hr = S_OK;

	return UnInitBus();
}


std_method_impl CAppViewImpl::InitBus()
{
	HRESULT hr = S_OK;

	m_pRot->SendMessage(CLSID_CObjectRun, CLSID_CObjectLoader, 0, 0, NULL);

	hr = m_pRot->GetObject(CLSID_IMsgBus, IID_IMsgBus, (IBase**)&m_pIMsgBus);
	rc_assert(hr == S_OK, E_FAIL)

	hr = m_pIMsgBus->CreateNode(NDC_BROKER, (IBase**)&m_pIBroker);
	rc_assert(hr == S_OK, E_FAIL)

	m_pIBroker->Listen("127.0.0.1:8080", s_strlen("127.0.0.1:8080"), 5);

	hr = m_pIMsgBus->CreateNode(NDC_CONSUMER, (IBase**)&m_pIConsumer);
	rc_assert(hr == S_OK, E_FAIL)

	m_pIConsumer->SetId(_inet_addr_v4("255.254.253.252"));
	m_pIConsumer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

	hr = m_pIMsgBus->CreateNode(NDC_PRODUCER, (IBase**)&m_pIProducer);
	rc_assert(hr == S_OK, E_FAIL)

	m_pIProducer->SetId(_inet_addr_v4("255.254.253.251"));
	m_pIProducer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

	hr = m_pIMsgBus->CreateNode(NDC_AGENT, (IBase**)&m_pIAgent);
	rc_assert(hr == S_OK, E_FAIL)

	m_pIAgent->SetId(_inet_addr_v4("255.254.253.250"));
	m_pIAgent->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

	int sec = 0;
#if (TARGET_OS == OS_WINDOWS)
	sec = 0;
#elif (TARGET_OS == OS_POSIX)
	sec = 10;
#elif (TARGET_OS == OS_DARWIN)
	sec = 10;
#endif
	return m_pIBroker->Dispatch(sec);
}
std_method_impl CAppViewImpl::UnInitBus()
{

	rc_assert(m_pIBroker.m_p != NULL, E_FAIL)
	m_pIBroker->Close();

	rc_assert(m_pIConsumer.m_p != NULL, E_FAIL)
	m_pIConsumer->Close();

	rc_assert(m_pIProducer.m_p != NULL, E_FAIL)
	m_pIProducer->Close();

	rc_assert(m_pIAgent.m_p != NULL, E_FAIL)
	m_pIAgent->Close();


	m_pIMsgBus.dispose();

	m_pIBroker.dispose();

	m_pIConsumer.dispose();

	m_pIAgent.dispose();

	m_pIProducer.dispose();

	return S_OK;
}
